// pages/install-report/index.js
import { getMonthStr } from "../../utils/util";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    monthShow: false,
    hasMore: true,
    report_month: "",
    report_type: "",
    param: {
      page: 1,
      pageSize: 10,
    },
  },

  getMore() {
    const { param, hasMore } = this.data;

    if (!hasMore) return;

    param.page++;

    this.setData({
      param,
    });

    this.getData();
  },

  handleMonthChange(event) {
    const { value } = event.detail;
    const { param } = this.data;

    param.page = 1;

    this.setData({
      report_month: value,
      param,
      list: [],
    });

    this.getData();
  },

  handleToDetail(event) {
    const { id } = event.currentTarget.dataset;

    wx.navigateTo({
      url: "/pages/submit-install-report/index?id=" + id + "&hide=1",
    });
  },

  getData() {
    const { param, report_month, report_type } = this.data;

    const params = {
      report_month: report_month || getMonthStr(),
      report_type,
      ...param,
    };

    wx.ajax.post("queryDataReportPage.action", params, true).then((res) => {
      // console.log(res);
      if (res.success) {
        const result = res.result;
        const oriList = this.data.list;

        this.setData({
          list: oriList.concat(result.pageList || []),
          hasMore: result.totalPages > result.page,
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      let type = "";

      switch (options.type) {
        case "4":
          type = 1;
          break;
        case "5":
          type = 2;
          break;
        case "6":
          type = 3;
          break;
        default:
          type = 1;
          break;
      }

      this.setData({
        report_type: type,
      });

      this.getData();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
